/**
 * 链表节点
 *
 * @author Kevin
 * @date 2018-11-26
 */
public class ListNode {

    /**
     * 节点值
     */
    public int val;

    /**
     * 下一个节点
     */
    public ListNode next;

    public ListNode() {
        this.next = null;
    }

    public ListNode(int val) {
        this.val = val;
    }

    /**
     * 链表节点的构造函数
     * 使用arr作为参数，创建一个链表，当前的ListNode为链表头节点
     */
    public ListNode(int[] arr) {
        if (arr == null || arr.length == 0) {
            throw new IllegalArgumentException("arr can not be empty.");
        }

        this.val = arr[0];
        ListNode cur = this;
        for (int i = 1; i < arr.length; i++) {
            cur.next = new ListNode(arr[i]);
            cur = cur.next;
        }
    }

    @Override
    public String toString() {

        StringBuilder sb = new StringBuilder();
        ListNode cur = this;
        while (cur != null) {
            sb.append(cur.val + "->");
            cur = cur.next;
        }
        sb.append("NULL");

        return sb.toString();
    }
}
